﻿/*
Copyright © 2010 François Karman

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

See <license.txt> for the complete license of the software.
 */

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;

namespace MSBuild.Mercurial
{
    /// <summary>
    /// Extracts the current <em>Changeset ID</em> and <em>revision number</em>
    /// of a Mercurial workspace.
    /// </summary>
    /// <remarks>
    /// <para>
    /// This method use the <c>hg summary</c> command.
    /// </para>
    /// <para>
    /// The name of the task conforms to the community standards
    /// as the MSBuild Community Tasks uses TfsVersion, SvnVersion
    /// as the name of the same kind of tasks.
    /// </para>
    /// </remarks>
    /// <example>
    /// <para>
    /// Initialize the <c>AssemblyRevision</c> property with the revision number
    /// of the folder containing the current MSBuild project:
    /// </para>
    /// <code><![CDATA[
    /// <HgVersion LocalPath="$(MSBuildProjectDirectory)" Timeout="5000">
    ///   <Output TaskParameter="Revision" PropertyName="AssemblyRevision" />
    /// </HgVersion>
    /// ]]></code>
    /// </example>
    [Command("summary")]
    public class HgVersion : AbstractHgTask
    {
        /// <summary>
        /// The regular expression associated with the "parent"
        /// line returned by <c>hg summary</c>.
        /// </summary>
        private static readonly Regex ParentRegex = new Regex(@"^parent: (\d+):([0-9a-f]+) (.*)$");

        /// <summary>
        /// Initializes a new instance of the HgVersion class.
        /// </summary>
        public HgVersion()
        {
            this.Timeout = DefaultTimeout;
        }

        /// <summary>
        /// Gets or sets the changeset identifier of the current workspace [Output].
        /// </summary>
        /// <seealso cref="Changeset"/>
        [Output]
        public string Changeset { get; set; }

        /// <summary>
        /// Gets or sets the revision number of the current workspace [Output].
        /// </summary>
        [Output]
        public int Revision { get; set; }

        /// <summary>
        /// Extracts the revision and changeset data from the output of the
        /// task execution process.
        /// </summary>
        /// <param name="outputs">
        /// The lines of the process output.
        /// </param>
        protected override void ManageOutput(ICollection<string> outputs)
        {
            foreach (string line in outputs)
            {
                Match match = ParentRegex.Match(line);
                if (match.Success)
                {
                    string revision = match.Groups[1].Value;
                    string changeset = match.Groups[2].Value;

                    this.Revision = Convert.ToInt32(revision, CultureInfo.InvariantCulture);
                    this.Changeset = changeset;
                }
            }
        }
    }
}
